package com;

import java.util.Set;
import java.util.TreeMap;

public class DataCollector {
	private static TreeMap<Integer, TreeMap<String, String>> mapData = null;
	private static TreeMap<Integer, TreeMap<String, TreeMap<String, String>>> countrySubregionData = null;

	public static String getData(Object country_id, Object region_name) {
		checkDataExists();
		Integer iCountry_id = null;
		try {
			iCountry_id = Integer.parseInt(country_id.toString());
		} catch (Exception ex) {
			return "[]";
		}
		TreeMap<String, String> data = mapData.get(iCountry_id);
		if (data == null || data.isEmpty())
			return "[]";
		String result = "[";

		Set<String> keys = data.keySet();
		String mData = "";
		for (String key : keys) {
			String value = data.get(key);
			if (mData.length() > 0)
				mData += ",";
			mData += "[\"" + key + "\"," + "\"" + value + "\"]";
		}
		result += mData;
		return result + "]";
	}

	public static void main(String[] args) {
		System.out.println(getData(1, ""));
	}

	private static void checkDataExists() {
		if (mapData != null && countrySubregionData != null)
			return;
		mapData = new TreeMap<Integer, TreeMap<String, String>>();
		countrySubregionData = new TreeMap<Integer, TreeMap<String, TreeMap<String, String>>>();

		TreeMap<String, String> localData = new TreeMap<String, String>();

		localData
				.put("Tbilisi",
						"4964479.74585424;5104406.67036957;5011326.67006168;5137544.80832204");
		localData
				.put("Mtskheta-Mtianeti",
						"4923141.01992121;5115099.56713812;5043947.59475102;5276534.32168495");
		localData
				.put("Kvemo Kartli",
						"4861411.96983919;5036792.852605;5044531.40013876;5139984.26051889");
		localData
				.put("Shida Kartli",
						"4834989.31674468;5121578.2687641;4961886.21719602;5256168.61411804");
		localData
				.put("Ajara",
						"4624997.95138117;5076294.49136224;4742369.90148228;5147436.55834895");
		localData
				.put("Apkhazeti",
						"4453727.39241658;5222749.7348055;4692011.90642376;5401644.7976862");
		localData
				.put("Samegrelo-zemo svaneTi",
						"4625233.13321683;5171272.15403414;4803377.71064881;5351178.78639194");
		localData
				.put("Guria",
						"4642952.3145858;5129035.03640943;4749836.92149254;5186387.74791738");
		localData
				.put("Imereti",
						"4699267.1932584;5132334.64019843;4874665.15281493;5246003.72114908");
		localData
				.put("Racha-Lechkumi-qvemo svaneTi",
						"4714981.03751453;5216014.96872879;4892175.75222264;5307115.08191041");
		localData
				.put("Samtskhe-Javakheti",
						"4729820.14340701;5028593.68909877;4894136.98677515;5153434.64922562");
		localData
				.put("Kakheti",
						"5014410.79297002;5020264.85210017;5202438.90493883;5247255.69454014");

		mapData.put(1, localData);

		localData = new TreeMap<String, String>();

		localData
				.put("Nakhchivan",
						"4982914.61055814;4699299.97324069;5137451.89096571;4836033.66855591");
		localData
				.put("Absheron",
						"5625616.74226517;4907594.37263707;5635178.13063608;4917531.41767745");
		localData
				.put("Yukhari-Karabakh",
						"5169019.21647217;4741852.9832146;5303316.10261872;4935944.44021665");
		localData
				.put("Lankaran",
						"5344626.12406625;4635186.82134015;5445893.1372039;4782232.39996838");
		localData
				.put("Daglig-Shirvan",
						"5313555.69185786;4904463.99718987;5483944.44415435;5021658.26685165");
		localData
				.put("Absheron",
						"5428291.91781377;4817959.9378331;5608200.10256177;5009520.29829573");
		localData
				.put("Kalbajar-Lachin",
						"5076045.87406038;4702828.24493469;5217813.64277839;4911746.30809208");
		localData
				.put("Ganja-Qazakh",
						"5011128.59784515;4903214.1692603;5227964.48013466;5082377.42952228");
		localData
				.put("Shaki-Zaqatala",
						"5140549.89251031;4959306.33061826;5346799.42941002;5148033.98684673");
		localData
				.put("Quba-Khachmaz",
						"5321659.25043292;4990985.4578491;5496380.38922914;5144240.41042191");
		localData
				.put("Aran",
						"5201942.34315525;4710043.24607593;5503747.92978227;5007735.07531051");
		localData
				.put("Baku city",
						"5006234.68274827;4817959.93779527;5635178.13063608;5026593.42866654");

		mapData.put(2, localData);

		putSubregionData(1, "Abkhazeti", "Afkhazeti",
				"4453727.39219878;5222749.73488586;4692011.90651965;5401644.79756415");
		putSubregionData(1, "Ajara", "Keda",
				"4647856.34920668;5085582.2361382;4689438.30728822;5116613.48376936");
		putSubregionData(1, "Ajara", "Khelvachauri",
				"4624997.95136708;5076294.49154824;4669089.46256146;5116106.61176312");
		putSubregionData(1, "Ajara", "Khulo",
				"4703601.72839033;5079982.04813528;4742369.90143062;5137554.23475106");
		putSubregionData(1, "Ajara", "KobuleTi",
				"4643411.76718044;5103079.22201861;4699527.32702333;5147436.55825007");
		putSubregionData(1, "Ajara", "Shuakhevi",
				"4686228.31785557;5076870.29564302;4733789.00141006;5132570.30136496");
		putSubregionData(1, "Guria", "Chokhatauri",
				"4689540.74783107;5130327.88087342;4749836.9213819;5173760.77307816");
		putSubregionData(1, "Guria", "Lanchkhuti",
				"4642952.31477671;5157597.6934892;4700507.59079161;5186387.74798245");
		putSubregionData(1, "Guria", "Ozurgeti",
				"4648481.29213158;5129035.03643085;4707904.9133833;5165734.36095311");
		putSubregionData(1, "Imereti", "Bagdati",
				"4747910.15186968;5132334.64025327;4800662.10760373;5190744.651544");
		putSubregionData(1, "Imereti", "Chiatura",
				"4798811.43926463;5179583.73857695;4837227.57378593;5226987.90358953");
		putSubregionData(1, "Imereti", "Kharagauli",
				"4786446.8373171;5135965.38533345;4842539.73183991;5185091.75949266");
		putSubregionData(1, "Imereti", "Khoni",
				"4711946.83578984;5195646.54846093;4746701.50079112;5246003.72103646");
		putSubregionData(1, "Imereti", "Sachkhere",
				"4815731.22723578;5174164.05001919;4874665.15290524;5230330.87749096");
		putSubregionData(1, "Imereti", "Samtredia",
				"4699267.193085;5160494.87970224;4735433.46765812;5202553.57567444");
		putSubregionData(1, "Imereti", "Terjola",
				"4755273.74321214;5183530.85634719;4801696.73749987;5206530.02822338");
		putSubregionData(1, "Imereti", "Tkibuli",
				"4757707.36741813;5200447.3990852;4802048.99614985;5234261.10537621");
		putSubregionData(1, "Imereti", "Tskaltubo",
				"4725713.1534042;5176517.48294303;4774210.74182516;5237868.72667367");
		putSubregionData(1, "Imereti", "Vani",
				"4719313.2239947;5133537.90656162;4765029.46411641;5181627.12020701");
		putSubregionData(1, "Imereti", "Zestafoni",
				"4760746.54110508;5165527.9725164;4812337.95032385;5196079.41454538");
		putSubregionData(1, "Kakheti", "Akhmeta",
				"5014618.18811814;5142624.25430544;5096406.65625129;5247255.69462936");
		putSubregionData(1, "Kakheti", "Dedoflistskaro",
				"5093635.34228599;5020264.85199965;5202438.90492406;5098422.77807768");
		putSubregionData(1, "Kakheti", "Gurjaani",
				"5066219.80398849;5091822.93133656;5118738.65043253;5146848.75613852");
		putSubregionData(1, "Kakheti", "Kvareli",
				"5071497.44814518;5131361.96077857;5134044.25047779;5180790.60539415");
		putSubregionData(1, "Kakheti", "Lagodekhi",
				"5110434.82426264;5105165.22312529;5168122.24738875;5163290.52777534");
		putSubregionData(1, "Kakheti", "Sagarejo",
				"5014410.79279906;5074077.31648458;5081038.73199823;5149774.17815159");
		putSubregionData(1, "Kakheti", "Sighnaghi",
				"5069036.7764297;5049761.74034892;5163489.05000415;5116722.38062791");
		putSubregionData(1, "Kakheti", "Telavi",
				"5040207.21914491;5131120.68706314;5092714.57206286;5205401.60090738");
		putSubregionData(1, "Mtskheta-Mtianeti", "Dusheti",
				"4949870.24744518;5151226.58764021;5043947.59467596;5274095.96360542");
		putSubregionData(1, "Mtskheta-Mtianeti", "Kazbegi",
				"4923141.01966964;5225859.56650777;4993031.37199205;5276534.3219189");
		putSubregionData(1, "Mtskheta-Mtianeti", "Mtskheta",
				"4950334.10277063;5115099.56690955;5016423.14311407;5160812.73223353");
		putSubregionData(1, "Mtskheta-Mtianeti", "Mtskheta",
				"4998276.81445897;5131686.02428823;4998369.64418812;5131779.32871414");
		putSubregionData(1, "Mtskheta-Mtianeti", "Tianeti",
				"4992548.56635563;5145090.49378065;5036878.89218286;5219188.7757816");
		putSubregionData(1, "Qvemo Kartli", "Bolnisi",
				"4925753.72774003;5039153.15132326;4985830.35606219;5087628.93471727");
		putSubregionData(1, "Qvemo Kartli", "Dmanisi",
				"4886054.96012562;5036792.85277663;4946642.15531827;5094874.69660704");
		putSubregionData(1, "Qvemo Kartli", "Gardabani",
				"4977367.77384487;5107236.38036632;4977770.04781395;5107383.61972289");
		putSubregionData(1, "Qvemo Kartli", "Gardabani",
				"4977770.04781395;5062635.47745264;5044531.4000028;5139984.26064662");
		putSubregionData(1, "Qvemo Kartli", "Marneuli",
				"4955830.39916837;5040524.32477522;5019893.8069644;5095104.81769933");
		putSubregionData(1, "Qvemo Kartli", "Tetritskaro",
				"4919690.4290102;5079244.33963142;4990597.02164396;5127480.07654626");
		putSubregionData(1, "Qvemo Kartli", "Tsalka",
				"4861411.96975428;5083495.15007366;4926027.3472691;5125507.76424592");
		putSubregionData(1, "Racha-Lechkumi-Qvemo Svaneti", "Ambroloauri",
				"4768723.78227157;5216014.9688403;4826975.76808225;5286314.9675361");
		putSubregionData(1, "Racha-Lechkumi-Qvemo Svaneti", "Cageri",
				"4734256.00913731;5230592.47778963;4798715.54583878;5277223.02984796");
		putSubregionData(1, "Racha-Lechkumi-Qvemo Svaneti", "Lentekhi",
				"4714981.03754959;5260680.50120786;4823945.35853761;5307115.08184612");
		putSubregionData(1, "Racha-Lechkumi-Qvemo Svaneti", "Oni",
				"4813010.53546808;5226484.81668967;4892175.75222594;5296530.71979605");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Abasha",
				"4678788.77638936;5177132.3415623;4717387.35585147;5212107.51818901");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Chkhorotsku",
				"4676687.72152707;5221206.42595048;4722611.33226862;5287918.75565677");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Khobi",
				"4632166.54024679;5181343.04150993;4682718.30760307;5231601.85514554");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Martvili",
				"4694766.20027217;5205232.72229473;4744132.63268778;5279102.83653195");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Mestia",
				"4660508.69623891;5283166.87102683;4803377.71060359;5351178.7864659");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Poti",
				"4634643.26212945;5171272.15391497;4649519.58813303;5191982.6521045");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Senaki",
				"4660785.66844634;5182499.63368845;4705460.97296756;5229219.34806757");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Tsalendjikha",
				"4663249.54005687;5237531.30481646;4703711.91720015;5291076.64071081");
		putSubregionData(1, "Samegrelo-Zemo Svaneti", "Zugdidi",
				"4625233.13332936;5208357.7449281;4679601.29296456;5256161.08731538");
		putSubregionData(1, "Samtskhe-Javakheti", "Adigeni",
				"4729820.14347887;5097960.27456617;4782740.75996;5137752.80155814");
		putSubregionData(1, "Samtskhe-Javakheti", "Akhalkalaki",
				"4800790.64210401;5037961.60275145;4867339.06665515;5113636.34242284");
		putSubregionData(1, "Samtskhe-Javakheti", "Akhaltsikhe",
				"4764212.7090711;5075841.85798812;4817522.79387422;5137072.02654675");
		putSubregionData(1, "Samtskhe-Javakheti", "Aspindza",
				"4783080.19073863;5057407.35260891;4836023.80478677;5116558.93028258");
		putSubregionData(1, "Samtskhe-Javakheti", "Borjomi",
				"4800948.4632927;5100276.47288273;4886374.27777655;5153434.64935598");
		putSubregionData(1, "Samtskhe-Javakheti", "Ninotsminda",
				"4834676.49070714;5028593.68904512;4894136.98664508;5091652.58906221");
		putSubregionData(1, "Shida Kartli", "Gori",
				"4874334.26229809;5122045.25003796;4939961.06618606;5234293.49501388");
		putSubregionData(1, "Shida Kartli", "Kareli",
				"4853343.46347043;5130032.56819483;4904466.33724265;5213665.80505397");
		putSubregionData(1, "Shida Kartli", "Kaspi",
				"4912921.14916012;5121578.26863815;4961886.21717929;5169314.14175764");
		putSubregionData(1, "Shida Kartli", "Khashuri",
				"4834989.31664597;5137390.9600414;4872331.96839306;5191281.59952242");
		putSubregionData(1, "Tbilisi", "Tbilisi",
				"4964479.74593068;5104406.67040182;5011326.66984784;5137544.80841591");

	
		// ="localData.put("""&E1&""""&","""&A1&";"&B1&";"&C1&";"&D1&""""&");"

		// select
		// state_eng_id,state_eng,region_eng,distr_eng,ST_XMin(the_geom),ST_YMin
		// (the_geom),ST_XMax(the_geom),ST_YMax(the_geom) from ( select case
		// when state_eng='Georgia' then 1 when state_eng='Azerbaijan' then 2
		// when state_eng='Armenia' then 3 end state_eng_id,
		// state_eng,region_eng,distr_eng,ST_TRANSFORM(the_geom,900913) the_geom
		// from subregions ) k

	}

	private static void putSubregionData(int country_id, String region,
			String sub_region, String data) {
		TreeMap<String, TreeMap<String, String>> regions = countrySubregionData
				.get(country_id);
		if (regions == null) {
			regions = new TreeMap<String, TreeMap<String, String>>();
			countrySubregionData.put(country_id, regions);
		}
		TreeMap<String, String> sub_regions = regions.get(region);
		if (sub_regions == null) {
			sub_regions = new TreeMap<String, String>();
			regions.put(region, sub_regions);
		}
		sub_regions.put(sub_region, data);
	}

}
